module.exports = function ($scope, $state, $resource, $modal, toaster) {
    $scope.myKeyup = function (e) {
        //IE 编码包含在window.event.keyCode中，Firefox或Safari 包含在event.which中
        var keycode = window.event ? e.keyCode : e.which;
        if (keycode == 13) {
            $scope.loadlist();
        }
    };

    function date2str(d) {
        if (d === undefined) {
            return "";
        }
        var month = (d.getMonth() + 1).toString();
        var day = d.getDate().toString();
        if (month.length == 1) month = '0' + month;
        if (day.length == 1) day = '0' + day;
        return d.getFullYear() + "-" + month + "-" + day;
    }

    $scope.dateOpen = function ($event, item) {
        $event.preventDefault();
        $event.stopPropagation();
        item.opened = true;
    };

    $scope.period_start = {
        'value': date2str(new Date()),
        // 'value': '',
        'opened': false
    }

    $scope.period_end = {
        'value': date2str(new Date()),
        // 'value': '',
        'opened': false
    }

    $scope.searchform = {};

    $scope.loadlist = function () {
        if (!$scope.period_start.value || !$scope.period_end.value) {
            toaster.warning({ title: '', body: '请选择开始和结束日期' })
            return false;
        }
        $scope.searchform.start_time = (typeof $scope.period_start.value === 'string' ? $scope.period_start.value : date2str($scope.period_start.value));
        $scope.searchform.end_time = (typeof $scope.period_end.value === 'string' ? $scope.period_end.value : date2str($scope.period_end.value));
        $resource('/api/as/puc/citydiscount/findAllPriceList', {}, {}).save($scope.searchform, function (res) {
            if (res.errcode === 0) {
                $scope.recordList = res.data;
                $scope.total = 0;
                for (let index = 0; index < $scope.recordList.length; index++) {
                    const element = $scope.recordList[index];
                    $scope.total += element.allprice;
                }
                $scope.total = $scope.total.toFixed(2);
                $scope.recordList.push({
                    date: '总计',
                    allprice: $scope.total
                })
            } else {
                toaster.error({ title: '', body: res.errmsg });
            }
        });
    }
    $scope.loadlist();

    $scope.info = function (item) {
        var modalInstance = $modal.open({
            template: require('../views/statisticsDiscountInfo.html'),
            controller: 'statisticsDiscountInfo',
            size: 'lg',
            resolve: {
                item: function () {
                    return item;
                }
            }
        });
        modalInstance.result.then(function () {
            $scope.loadlist();
        }, function () {
        });
    };

};